package cn.tworice.admin.web;

import cn.tworice.admin.service.LogService;
import cn.tworice.admin.service.SystemService;
import cn.tworice.common.entity.Admin;
import cn.tworice.common.entity.AdminLog;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @Author 二饭
 * @Email 1786705138@qq.com
 * @Date 2021/12/4 20:16
 */
@RestController
@RequestMapping("admin/admin")
@Slf4j
@CrossOrigin
public class AdminController {
    @Autowired
    private SystemService systemService;

    @Autowired
    private LogService logService;

    @GetMapping("list")
    @ApiOperation("获取管理员列表")
    public RequestResult list() {
        RequestResult result = new RequestResult();
        List<Admin> adminList = systemService.getAdminList();
        result.setCode(new StatusCode(200, "获取所有管理员列表"));
        result.appendData("adminData", adminList);
        return result;
    }

    @PostMapping("add")
    @ApiOperation("添加一个管理员账号")
    public RequestResult add(Admin admin, HttpServletRequest request) {
        // 添加日志
        logService.addAdminLog(request.getHeader("adminID"),"添加/更新一个管理员账号信息");

        RequestResult result = new RequestResult();
        List<Admin> adminList = systemService.addAdmin(admin);
        result.setCode(new StatusCode(200, "添加一个管理员账号，返回添加账号后的列表"));
        result.appendData("adminData", adminList);
        return result;
    }

    @DeleteMapping("del")
    @ApiOperation("删除管理员账号")
    public RequestResult del(String[] ids,HttpServletRequest request) {
        logService.addAdminLog(request.getHeader("adminID"),"删除人员账号账号-"+ Arrays.toString(ids));

        RequestResult result = new RequestResult();
        List<Admin> adminList = systemService.delAdminByIds(ids);
        result.setCode(new StatusCode(200,"删除管理员账号"));
        result.appendData("adminData", adminList);
        return result;
    }

    @PutMapping("update")
    @ApiOperation("更新管理员密码")
    public RequestResult update(Admin admin,HttpServletRequest request){
        logService.addAdminLog(request.getHeader("adminID"),"更新账号密码");

        RequestResult result = new RequestResult();
        boolean b=systemService.updateAdminInfo(admin);
        result.setCode(new StatusCode(200,"更新管理员信息"));
        result.appendData("status", b);
        return result;
    }
}
